if (!require("pacman")) install.packages("pacman")
pacman::p_load(readr, dplyr, tibble, janitor, QCA)

#database
matriz <- data.frame(
  pais = c("AUS", "BE", "BUL", "CRO", "CYP", "CZ", "EST", "FIN",
           "HUN", "IRL", "LAT", "LITH", "LUX", "MAL", "NL", "POL",
           "POR", "ROM", "SLO", "SLE", "ESP", "SV"),
  NCoop = c(98.00, 69.33, 95.39, 91.53, 55.31, 99.55, 55.32, 4.67, 100.00, -48.00, 31.81, 42.77, 1.44, -28.24,
            54.20, 100.00, 48.09, 82.58, 98.23, 55.38, 86.28, 19.09),
  Rgov = c(5146.0, 6114.0, 7493.4, 5939.0, 8760.0, 7899.1, 4250.7, 5931.6, 10877.0, 5110.0, 6846.0, 5596.4, 
           4380.0, 6414.0, 8676.0, 8500.3, 4736.6, 5950.2, 9992.0, 6278.0, 8322.0, 6892.0),
  POfav = c(0.65, 0.69, 0.27, 0.69, 0.80, 0.25, 0.46, 0.72, 0.28, 0.79, 0.37, 0.51, 0.77, 0.76, 0.87, 0.53, 
            0.69, 0.46, 0.32, 0.54, 0.83, 0.92),
  PNref= c(6.600, 2.290, 0.810, 0.000, 3.600, 0.000, 0.120, 2.010, 0.310, 1.440, 0.190, 0.480, 3.030, 18.960, 
           4.540, 0.420, 0.090, 0.080, 0.000, 0.250, 0.100, 12.110)
)

matriz
#Calibrations

#Thresholds
find_NCoop
find_Rgov
find_POfav
find_PNref

#dataset
matriz_calib <- data.frame(
  NCoop = round(calibrate(matriz$NCoop, type = "fuzzy", thresholds = find_NCoop), 2),
  Rgov = round(calibrate(matriz$Rgov, type = "fuzzy", thresholds = find_Rgov), 2),
  POfav = round(calibrate(matriz$POfav, type = "fuzzy", thresholds = find_POfav), 2),
  PNref = round(calibrate(matriz$PNref, type = "fuzzy", thresholds = find_PNref), 2),
  row.names = matriz$pais
)


# Analysis ----
# Necessity
nec <- superSubset(
  matriz_calib,
  outcome = "NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  relation = "necessity",
  incl.cut = .0,
  sort.by = "inclN"
)
nec

# TT
tt <- truthTable(
  matriz_calib,
  outcome = "NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  relation = "suf",
  show.cases = T,
  incl.cut = 0.8,
  sort.by = "incl"
)
tt

# Complex Solution
sc <- (sc <- minimize(
  tt,
  outcome = "NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  details = T,
  show.cases = T
))
sc

# Enhanced Parsimonious Solution
enhanced <- findRows(obj = tt, type = 2) # pressupostos simplificadores contraditorios para excluir
enhanced

sp <- minimize(
  tt,
  outcome = "NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  exclude = enhanced,
  include = "?",
  details = T,
  show.cases = T
)

sp

# Intermediate Solution
si <- minimize(
  tt,
  exclude = enhanced,
  include = "?",
  dir.exp = "1, 0, 0",
  details = T,
  show.cases = T
)
si
si$i.sol$C1P1$EC # nenhum contrafactual bom

#Graphs
xysuf<-XYplot(matriz_calib, y = "NCoop", x = "Rgov*~PNref + ~POfav*~PNref",
       jitter = T, relation = "suf", clabels = rownames(matriz_calib))

#OUTOME NEGATION ------------------------------------

# Necessity
necn <-superSubset(
  matriz_calib,
  outcome = "~NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  relation = "necessity",
  incl.cut = .0,
  sort.by = "inclN"
)
necn

# TT
ttn <- truthTable(
  matriz_calib,
  outcome = "~NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  relation = "suf",
  show.cases = T,
  incl.cut = 0.8,
  sort.by = "incl"
)
ttn

# Complex Solution
scn <- minimize(
  ttn,
  outcome = "~NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  details = T,
  show.cases = T
)
scn

# Enhanced Parsimonious Solution
enhancedn <- findRows(obj = tt, type = 2) # pressupostos simplificadores contraditorios para excluir
enhancedn

spn <- minimize(
  ttn,
  outcome = "~NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  exclude = enhancedn,
  include = "?",
  details = T,
  show.cases = T
)
spn

# Intermediate Solution
sin <- minimize(
  ttn,
  outcome = "~NCoop",
  conditions = c("Rgov", "POfav", "PNref"),
  exclude = enhancedn,
  include = "?",
  dir.exp = "1, 0, 0",
  details = T,
  show.cases = T
)
sin

#Graphs
xysufn <- XYplot(matriz_calib, y = "~NCoop", x = "POfav * PNref",
       jitter = T, relation = "suf", clabels = rownames(matriz_calib))

runGUI()
